package com.simtop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.dao.U3DSportDao;
import com.simtop.pojo.Sport;
import com.simtop.service.U3DSportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class U3DSportServiceImpl implements U3DSportService {
    @Autowired
    private U3DSportDao u3DSportDao;

    @Override
    public ServerResponse<String> add(Sport sport) {
        int resultCount = u3DSportDao.add(sport);
        if (resultCount != 1){
            return ServerResponse.createByErrorMsg("传输记录失败，请检查");
        }
        System.out.println("U3DSport："+sport);
        System.out.println("实验id为" + sport.getId() + "的记录成功");
        return ServerResponse.createBySuccessMsg("传输记录成功");
    }

    @Override
    public ServerResponse<Sport> findById(Integer id) {
        Sport sport = u3DSportDao.findById(id);
        return ServerResponse.createBySuccess(sport);
    }

    @Override
    public ServerResponse<PageInfo<Sport>> findResult(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Sport> sports =u3DSportDao.findResult();
        PageInfo<Sport> sportPageInfo = new PageInfo<>(sports);
        return ServerResponse.createBySuccess(sportPageInfo);
    }

    @Override
    public ServerResponse<String> deleteById(int id) {
        int count = u3DSportDao.deleteById(id);
        if (count == 1){
            return ServerResponse.createBySuccessMsg("删除记录成功");
        }
        return ServerResponse.createByErrorMsg("删除记录失败");
    }

    @Override
    public ServerResponse<PageInfo<Sport>> findByName(String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Sport> sports = u3DSportDao.findByName(name);
        PageInfo<Sport> sportPageInfo = new PageInfo<>(sports);
        return ServerResponse.createBySuccess(sportPageInfo);
    }

    @Override
    public ServerResponse<Integer> findUserAccount() {
        return ServerResponse.createBySuccess(Integer.valueOf(u3DSportDao.findUserAccount()));
    }

    @Override
    public ServerResponse<Integer> expAcct() {
        return ServerResponse.createBySuccess(Integer.valueOf(u3DSportDao.expAcct()));
    }

}
